/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

function initNE() {
  loadTopicList('ne');
  
  document.title = 'SEPIA: '+msg( lang, 'msg_ne' );
  
  loadHeaderSubSub( lang );
  
  if ( isMonolingual ) {
    $('msg_question_src' ).style.display = 'none';
    $('questionSrc'  ).style.display = 'none';
    $('msg_narrative_src').style.display = 'none';
    $('narrativeSrc' ).style.display = 'none';
  }
  
  loadMessages(lang);
}

function loadTopicNE( topicId ) {
  lastTopicId["ne"] = topicId;
  if ( topicId=='' ) {
    jQuery('#additional1').slideUp("slow",footer);
    jQuery('#additional2').slideUp("slow",footer);
  } else {
    jQuery('#additional1').slideUp("fast", function(){
      jQuery('#additional2').slideUp("fast", function(){
        var param = eval('$H({"action":"GetTopic","topicId": ' + topicId + ', "tool":"ne"})').toQueryString();
        var json = sync( param );
        loadTopicJsonIntoHTML(json);
        loadStaticAnswerText(json);
        loadDynamicAnswerNugget(json);
        
        $('done').checked = json.topics[0].done==1?true:false;
        jQuery('#additional1').slideDown("slow",footer);
        jQuery('#additional2').slideDown("slow",footer);
      });
    });
  }
}

function loadStaticAnswerText(json) {

  var table = '<table width="100%" border="0" cellspacing="0" cellpadding="5"><tr><td bgcolor="#99FFFF">\n';
  table += '<table width="100%"  border="0" cellspacing="0" cellpadding="1">\n';
  table += '<tr bgcolor="#00CCFF">\n';
  table += '<td><div id="msg_answer_text" align="center"></div></td>';
  table += '<td><div id="msg_doc_id" align="center"></div></td>';
  table += '</tr>\n';
  
  var answers = json.topics[0].answerTexts;
  
  //some topic may not have answer
  if (!!answers) {  
    for (var i = 0; i < answers.length; i++) {    
        var id = answers[i].documentId;
        var idlen = id.length;
        if ( idlen > 12 ) {
            id = id.substring(0,12)+"<br>"+id.substring(12,idlen);
        }
         
        var label = answers[i].label.length>0?'['+answers[i].label+']':'';
        var tr = '<tr valign="top" bgcolor="'+((i%2==1)?'#99ffff':'#ccffff')+'">\n';
        tr += '<td><div style="margin-bottom:10px">'+ label+ answers[i].answerBearingText +'</div></td>\n';
        tr += '<td>'+ id +'</td>\n';
        tr += '</tr>\n';
      table += tr;
    }
  }
  table += "</table>";
  table += '</td></tr></table>\n';
   
  $('answerText').innerHTML = table;
}

function loadDynamicAnswerNugget(json) {

  var nuggets = json.topics[0].answerNuggets;
  
  var table1 = '<table width="100%" border="0" cellspacing="0" cellpadding="5"><tr><td bgcolor="#95FFD5">\n';
  table1 += '<table width="100%" border="0" cellspacing="0" cellpadding="2">\n';
  table1 += '<tr bgcolor="#33CC99">\n';
  table1 += '<td colspan="2"><div id="msg_answer_nugget" align="center"></div></td>';
  table1 += '</tr>\n';
  
  var counter=0; // don't replace this with i
  for (var i = 0; nuggets && i < nuggets.length; i++) {
      if ( nuggets[i].isSimilar == 0 ) {
        var tr = '<tr valign="top" bgcolor="'+((counter++%2==1)?'#95FFD5':'#D5FFEE')+'">\n';
        tr += '<td>'+ nuggets[i].answerNugget +'</td>\n';
        tr += '<td align="right"><input class="msg_delete" type="button" value="DELETE" style="width:80px" onclick="deleteAnswerNugget('+nuggets[i].nuggetId+')"></td>\n';
        tr += '</tr>\n';
        table1 += tr;
      }
  }
   
  table1 += '<tr valign="top" bgcolor="#95FFD5">\n';
  table1 += '<td valign="middle"><input type="text" style="width:320px" id="newAnswerNugget" class="inputbox"></td>\n';
  table1 += '<td align="right"><input class="msg_add" type="button" value="ADD" style="width:80px" onclick="addAnswerNugget(0)"></td>\n';
  table1 += '</tr>\n';
  table1 += '</table>\n';
  table1 += '</td></tr></table>\n';
   
  var table2 = '<table width="100%" border="0" cellspacing="0" cellpadding="5"><tr><td bgcolor="#95FFD5">\n';
  table2 += '<table width="100%"  border="0" cellspacing="0" cellpadding="2">\n';
  table2 += '<tr bgcolor="#33CC99">\n';
  table2 += '<td colspan="2"><div id="msg_similar_nugget" align="center"></div></td>';
  table2 += '</tr>\n';

  var counter=0; // don't replace this with i
  for (var i = 0; nuggets && i < nuggets.length; i++) {
      if ( nuggets[i].isSimilar == 1 ) {
        var tr = '<tr valign="top" bgcolor="'+((counter++%2==1)?'#95FFD5':'#D5FFEE')+'">\n';
        tr += '<td>'+ nuggets[i].answerNugget +'</td>\n';
        tr += '<td align="right"><input class="msg_delete" type="button" value="DELETE" style="width:80px" onclick="deleteAnswerNugget('+nuggets[i].nuggetId+')"></td>\n';
        tr += '</tr>\n';
        table2 += tr;
      }
  }
   
  table2 += '<tr valign="top" bgcolor="#95FFD5">\n';
  table2 += '<td valign="middle"><input type="text" style="width:320px" id="newAnswerNugget2" class="inputbox"></td>\n';
  table2 += '<td align="right" valign="middle"><input class="msg_add" type="button" value="ADD" style="width:80px" onclick="addAnswerNugget(1)"></td>\n';
  table2 += '</tr>\n';
  table2 += '</table>\n';
  table2 += '</td></tr></table>\n';
  $('answerNuggetForm').innerHTML  = table1;
  $('answerNuggetForm2').innerHTML = table2;
  
  loadMessages( lang );
}

function addAnswerNugget( is_similar ) {
  var answerNugget = "";
  if ( is_similar == 0 ) { 
    answerNugget = es( $('newAnswerNugget').value );
  } else {
    answerNugget = es( $('newAnswerNugget2').value );
  }
  
  var param = [];
  param.action       = "AddNugget";
  param.topicId      = $('topicId').value;
  param.isSimilar    = is_similar;
  param.answerNugget = answerNugget;
  param.tool         = "ne";
  
  var json = sync(param);
  loadDynamicAnswerNugget(json);
  log("ADD ANSWER NUGGET","topic id="+$('topicId').value+", answer nugget="+answerNugget);
  showMessage( msg_suc_nug_add ); 
}

function deleteAnswerNugget(nuggetId) {
  if ( !deleteConfirmed() ) return;
  var param = [];
  param.action   = "DeleteNugget";
  param.nuggetId = nuggetId;
  param.topicId  = $('topicId').value;
  param.tool     = "ne";
  
  var json = sync(param);
    loadDynamicAnswerNugget(json);
    log("DELETE ANSWER NUGGET","topic id="+$('topicId').value+", nugget id="+nuggetId);
    showMessage( msg_suc_nug_del );
}
